<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu router :collapse = "isCollapse" :collapse-transition="false" :unique-opened="true" background-color="#323743" text-color="#fff" active-text-color="#409eff" :default-active='activePath'>
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单区域 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单区域 -->
            <el-menu-item :index=" '/'+subitem.path + ''" v-for="subitem in item.children" :key="subitem.id" @click="saveNavState('/'+subitem.path)">
              <i class="el-icon-menu"></i>
              <span>{{item.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created: function() {
      this.getMenuList()
      this.activePath = window.sessionStorage.getItem('activePath')
    },
  data: function() {
    return {
      // 用户存放左侧菜单数据
      menuList: [],
      iconObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao',
      },
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    logout: function() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    getMenuList: async function() {
      const { data: res } = await this.$http.get('menus')
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      console.log(res)
    },
    toggleCollapse: function() {
      this.isCollapse = !this.isCollapse
    },
    saveNavState: function(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath;
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  background-color: #373d3f;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  > div {
    display: flex;
    align-items: center;
    span {
      color: #fff;
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #323743;
}
.el-main {
  background-color: #e9edf0;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border-right: 0;
}
.toggle-button {
  height: 20px;
  color: #fff;
  background-color: #485162;
  text-align: center;
  font-size: 12px;
  line-height: 20px;
}
</style>
